
<template>
  <div>

    <avue-crud
      ref="crud"
      :page="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :summary-method="(e)=>summaryMethod(e, tableOption)"
      @on-load="getList"
    >
      <template
        slot="errMsg"
        slot-scope="scope"
      >
        <span style="color: red">{{scope.row.errMsg}}</span>
      </template>
    </avue-crud>
    <div style="text-align: center; margin: 40px">

      <template v-if="mode === 'verify'">
        <el-button
          :loading="uploading"
          @click="handleCancelData"
          :disabled="result"
          v-if="result && result==='reject'"
        >取 消</el-button>
        <el-button
          :loading="uploading"
          type="primary"
          @click="handleConfirmData"
          :disabled="result"
          v-if="result && result==='pass'"
        >确 定</el-button>
      </template>
      <template v-else>
        <el-button @click="$emit('close')">返 回</el-button>
      </template>
    </div>
  </div>
</template>

<script>
import { tables } from "@/api/rca/swckbankverifypayment";
import { confirmTableConfigMap } from "@/const/crud/rca/swckbankverifypayment";
import { dateFormat } from '@/util/date'
import { summaryMethod } from '@/const/crud/rca/common'
import { mapGetters } from "vuex";

export default {
  name: "TempData",
  data () {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },

      tableLoading: false,
      uploading: false,
      summaryMethod,
    };
  },
  props: ["currentTable", "mode", "query", "result", "url"],
  watch: {
    currentTable (val) {

              this.page.currentPage = 1
              this.getList(this.page);
    }
  },
  computed: {
    tableOption () {
      const { currentTable } = this;
      return confirmTableConfigMap[currentTable].option;
    },
  },
  methods: {
    getList (page, params) {
      this.tableLoading = true;
      tables.viewDiff(this.url)
        .then((response) => {
          this.tableData = confirmTableConfigMap[this.currentTable].dataFormat(response.data.data);
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },

    async handleCancelData () {
      this.$confirm("此操作将撤销当前上传数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.uploading = true;
        try {
          await tables.cancel({
            verfiyConfirmFlag: this.currentTable,
          });
          this.$message.success("操作成功");
          this.handleNext('pass');
        } finally {
          this.uploading = false;
        }
      });
    },
    async handleConfirmData () {
      this.$confirm("当前上传数据将立即生效, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.uploading = true;
        try {
          await tables.save({
            verfiyConfirmFlag: this.currentTable,
          });
          this.$message.success("操作成功");
          this.handleNext('reject');
        } finally {
          this.uploading = false;
        }
      });
    },
    handleNext (val) {
      this.$emit("handleResult", val);
    }
  },
};
</script>
